/*******************************************************************************
 * Copyright (c) 2011 Prometheus Consulting
 *
 *     Licensed under the Apache License, Version 2.0 (the "License"); you may
 *     not use this file except in compliance with the License. You may obtain
 *     a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *     WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 *     License for the specific language governing permissions and limitations
 *     under the License.
 *******************************************************************************/
package nz.co.senanque.addressbook;

import nz.co.senanque.addressbook.instances.Person;
import nz.co.senanque.vaadinsupport.hibernatecontainer.HbnContainerTransactional;
import nz.co.senanque.vaadinsupport.permissionmanager.PermissionManager;
import nz.co.senanque.vaadinsupport.tableeditor.TableEditorLayout;
import nz.co.senanque.vaadinsupport.viewmanager.HelpLayout;
import nz.co.senanque.vaadinsupport.viewmanager.ViewManaged;
import nz.co.senanque.vaadinsupport.viewmanager.ViewManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.support.MessageSourceAccessor;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;

public class MainLayout extends CustomComponent implements InitializingBean, ViewManaged, MessageSourceAware {

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private VerticalLayout verticalLayout_1;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_1;
	@AutoGenerated
	private VerticalLayout verticalLayout_3;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_2;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_3;
	@AutoGenerated
	@Autowired private TableEditorLayout<Person> personTable;
	@AutoGenerated
	private Button helpButton;
	@AutoGenerated
	private Button logoutButton;

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":false,"snapToObject":false,"movingGuides":false,"snappingDistance":10} */
	@Autowired PermissionManager m_permissionManager;
	private MessageSourceAccessor m_messageSourceAccessor;
	private ViewManager m_viewManager;

    private final Logger logger = LoggerFactory.getLogger(MainLayout.class);
	private BeanFactory m_beanFactory;

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":false,"snapToObject":false,"movingGuides":false,"snappingDistance":10} */

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":false,"snapToObject":false,"movingGuides":false,"snappingDistance":10} */
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":false,"snapToObject":false,"movingGuides":false,"snappingDistance":10} */
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":false,"snapToObject":false,"movingGuides":false,"snappingDistance":10} */
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":false,"snapToObject":false,"movingGuides":false,"snappingDistance":10} */
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":false,"snapToObject":false,"movingGuides":false,"snappingDistance":10} */
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 * @param catalogue2 
	 */
	public MainLayout() {
	}

	public void afterPropertiesSet() throws Exception {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		
		helpButton.setCaption(m_messageSourceAccessor.getMessage("help"));
		helpButton.addListener(new ClickListener(){

			@Override
			public void buttonClick(ClickEvent event) {
				getViewManager().pushScreen(HelpLayout.class.getName());
			}});
		logoutButton.setCaption(m_messageSourceAccessor.getMessage("logout"));
		logoutButton.addListener(new ClickListener(){

			@Override
			public void buttonClick(ClickEvent event) {
				getViewManager().logout();
			}});
		
//		// When we complete a log in this event intialises the order
//		addListener(new RepaintRequestListener(){
//
//			public void repaintRequested(RepaintRequestEvent event) {
////				initializeOrder();
//				
//			}});

	}


	public void initializeOrder() {
	}

	public PermissionManager getPermissionManager() {
		return m_permissionManager;
	}

	public void setPermissionManager(PermissionManager permissionManager) {
		m_permissionManager = permissionManager;
	}
	public void setMessageSource(MessageSource messageSource) {
		m_messageSourceAccessor = new MessageSourceAccessor(messageSource);		
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// verticalLayout_1
		verticalLayout_1 = buildVerticalLayout_1();
		mainLayout.addComponent(verticalLayout_1);
		
		return mainLayout;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_1() {
		// common part: create layout
		verticalLayout_1 = new VerticalLayout();
		verticalLayout_1.setImmediate(false);
		verticalLayout_1.setWidth("-1px");
		verticalLayout_1.setHeight("-1px");
		verticalLayout_1.setMargin(false);
		
		// horizontalLayout_2
		horizontalLayout_2 = buildHorizontalLayout_2();
		verticalLayout_1.addComponent(horizontalLayout_2);
		
		// horizontalLayout_1
		horizontalLayout_1 = buildHorizontalLayout_1();
		verticalLayout_1.addComponent(horizontalLayout_1);
		
		return verticalLayout_1;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_2() {
		// common part: create layout
		horizontalLayout_2 = new HorizontalLayout();
		horizontalLayout_2.setImmediate(false);
		horizontalLayout_2.setWidth("100.0%");
		horizontalLayout_2.setHeight("-1px");
		horizontalLayout_2.setMargin(false);
		horizontalLayout_2.setSpacing(true);
		
		// horizontalLayout_3
		horizontalLayout_3 = buildHorizontalLayout_3();
		horizontalLayout_2.addComponent(horizontalLayout_3);
		horizontalLayout_2.setComponentAlignment(horizontalLayout_3,
				new Alignment(34));
		
		return horizontalLayout_2;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_3() {
		// common part: create layout
		horizontalLayout_3 = new HorizontalLayout();
		horizontalLayout_3.setImmediate(false);
		horizontalLayout_3.setWidth("-1px");
		horizontalLayout_3.setHeight("-1px");
		horizontalLayout_3.setMargin(false);
		
		// logoutButton
		logoutButton = new Button();
		logoutButton.setCaption("Button");
		logoutButton.setImmediate(true);
		logoutButton.setWidth("-1px");
		logoutButton.setHeight("-1px");
		horizontalLayout_3.addComponent(logoutButton);
		
		// helpButton
		helpButton = new Button();
		helpButton.setCaption("Button");
		helpButton.setImmediate(true);
		helpButton.setWidth("-1px");
		helpButton.setHeight("-1px");
		horizontalLayout_3.addComponent(helpButton);
			
		return horizontalLayout_3;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_1() {
		// common part: create layout
		horizontalLayout_1 = new HorizontalLayout();
		horizontalLayout_1.setImmediate(false);
		horizontalLayout_1.setWidth("-1px");
		horizontalLayout_1.setHeight("-1px");
		horizontalLayout_1.setMargin(false);
		
		
		// verticalLayout_3
		verticalLayout_3 = buildVerticalLayout_3();
		horizontalLayout_1.addComponent(verticalLayout_3);
		horizontalLayout_1.setComponentAlignment(verticalLayout_3, Alignment.MIDDLE_CENTER);
		
		return horizontalLayout_1;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_3() {
		// common part: create layout
		verticalLayout_3 = new VerticalLayout();
		verticalLayout_3.setImmediate(false);
		verticalLayout_3.setWidth("-1px");
		verticalLayout_3.setHeight("-1px");
		verticalLayout_3.setMargin(true);
		verticalLayout_3.addComponent((Component) personTable);
		verticalLayout_3.setComponentAlignment(personTable, Alignment.MIDDLE_CENTER);
		
		return verticalLayout_3;
	}

	public ViewManager getViewManager() {
		return m_viewManager;
	}

	public void setViewManager(ViewManager viewManager) {
		m_viewManager = viewManager;
	}

	protected TableEditorLayout<Person> getPersonTable() {
		return personTable;
	}

	protected void setPersonTable(TableEditorLayout<Person> personTable) {
		this.personTable = personTable;
	}

}
